﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.Data.Dynamic;
using EPiServer.Core;
using EPiServer.Data;

namespace EPiServer.DdsPageProvider
{
    internal static class Utility
    {
        public static void UpdateWith(this PropertyBag bag, PageData page)
        {
            foreach (var mappingName in bag.Keys.ToList())
			{
				if (mappingName == "NodeId")
					continue;

                bag[mappingName] = GetAndConvertForStorage(page, mappingName);
            }
        }

        private static object GetAndConvertForStorage(PageData page, string mappingName)
        {
            var value = page[mappingName];
			if (value == null && mappingName.Contains('_'))
				value = page[mappingName.FromStoreString()];

            if (value is PageReference)
                value = ((PageReference)value).ToLinkString();
			return value;
        }

        public static string ToLinkString(this PageReference pageLink)
        {
            if (PageReference.IsNullOrEmpty(pageLink))
                return null;
            return "PL_" + pageLink.ToString();
        }

        public static void UpdateWith(this Node node, PageData page)
        {
            node.ArchiveLink = page.ArchiveLink.ToLinkString();
            node.ParentLink = page.ParentLink.ToLinkString();

            //node.ACL = page.ACL;
            node.Changed = page.Changed;
            node.ChangedBy = page.ChangedBy;
            node.Created = page.Created;
            node.CreatedBy = page.CreatedBy;
            node.IsDeleted = page.IsDeleted;
            node.LanguageBranch = page.LanguageBranch;
            node.LanguageID = page.LanguageID;
            node.LinkType = page.LinkType;
            node.LinkURL = page.LinkURL;
            node.MasterLanguageBranch = page.MasterLanguageBranch;
            node.PageName = page.PageName;
            node.PageTypeID = page.PageTypeID;
            node.PageTypeName = page.PageTypeName;
            node.PendingArchive = page.PendingArchive;
            node.PendingPublish = page.PendingPublish;
            node.Saved = page.Saved;
            if (page.StartPublish > PropertyDate.MinValue)
                node.StartPublish = page.StartPublish;
            else
                node.StartPublish = null;
            node.StaticLinkURL = page.StaticLinkURL;
            node.Status = page.Status;
            if (page.StopPublish < PropertyDate.MaxValue)
                node.StopPublish = page.StopPublish;
            else
                node.StopPublish = null;
            node.URLSegment = page.URLSegment;
            node.VisibleInMenu = page.VisibleInMenu;
            node.WorkPageID = page.WorkPageID;
			node.PeerOrder = (int)page["PagePeerOrder"];
			node.ChildOrderRule = (int)page["PageChildOrderRule"];
        }

        public static T Peek<T>(this IEnumerable<T> items)
        {
            return items.Take(1).AsEnumerable().FirstOrDefault();
        }

		public static string CurrentUser()
		{
			// HACK
			return HttpContext.Current != null ? HttpContext.Current.User.Identity.Name : System.Threading.Thread.CurrentPrincipal.Identity.Name;
		}

		public static string ToStoreString(this string propertyString)
		{
			return propertyString.Replace('-', '_');
		}

		public static string FromStoreString(this string propertyString)
		{
			return propertyString.Replace('_', '-');
		}
    }
}
